//
//  SettingView.swift
//  IdleFish
//
//  Created by Ben on 2021/12/12.
//

import SwiftUI

enum SettingAction {
    case user                           // 个人资料设置
    case shippingAddress                // 收货地址设置
    case paySetting                     // 支付设置
    case account                        // 账号
    case blackList                      // 黑名单
    case parentModel                    // 长辈模式
    case newMessageNotif                // 新消息通知
    case autoAnwser                     // 所有宝贝自动回复
    case imageQuality                   // 图片质量设置
    case autoPlayVideo                  // 自动播放视频设置
    case speech                         // 语音电话设置
    case messageNotifSound              // 消息提示音设置
    case privacy                        // 隐私
    case aboutUs                        // 关于我们
    case recommendToFrined              // 推荐给朋友
    case idleFishPact                   // 闲鱼公约
    case safeCenter                     // 安全中心
    case clearCache                     // 清除缓存
    case logout                         // 退出登录
    
    var title: String {
        switch self {
        case .user:
            return "个人资料设置"
        case .shippingAddress:
            return "收货地址"
        case .paySetting:
            return "支付设置"
        case .account:
            return "账号"
        case .blackList:
            return "黑名单"
        case .parentModel:
            return "长辈模式"
        case .newMessageNotif:
            return "新消息通知"
        case .autoAnwser:
            return "所有宝贝自动回复"
        case .imageQuality:
            return "图片质量设置"
        case .autoPlayVideo:
            return "自动播放视频设置"
        case .speech:
            return "语音电话设置"
        case .messageNotifSound:
            return "消息提示音设置"
        case .privacy:
            return "隐私"
        case .aboutUs:
            return "关于闲鱼"
        case .recommendToFrined:
            return "把闲鱼推荐给朋友"
        case .idleFishPact:
            return "闲鱼社区公约"
        case .safeCenter:
            return "安全中心"
        case .clearCache:
            return "清除缓存"
        case .logout:
            return "退出登录"
        }
    }
}

struct SettingView: View {
    
    @Environment(\.presentationMode) private var presentationModel
    
    @EnvironmentObject var customTabbarVM: CustomTabbarViewModel
    
    
    private let settingActionList: [[SettingAction]] = [
        [.user, .shippingAddress, .paySetting, .account, .blackList, .parentModel],
        [.newMessageNotif, .autoAnwser, .imageQuality, .autoPlayVideo, .speech, .messageNotifSound, .privacy],
        [.aboutUs, .recommendToFrined, .idleFishPact, .safeCenter],
        [.clearCache],
        [.logout],

    ]
    
    var body: some View {
        
        SettingNavView()
        
        SettingTableView()
    }
    
    @ViewBuilder
    private func execSettingAction(settingAction: SettingAction) -> some View {
        switch settingAction {
        case .user:
            UserDataView()
        default:
            EmptyView()
        }
    }
    
    private func SettingNavView() -> some View {
        
        CustomNavView(title: "设置") {
            withAnimation {
                customTabbarVM.isAcvite.toggle()
                customTabbarVM.atFront = true
            }
        }
    }
    
    private func SettingTableView() -> some View {
        ScrollView {
            
            Color(hex: 0xF1F4F7)
                .frame(height: 20)
            
            
            VStack(alignment: .leading, spacing: 0) {
                ForEach(settingActionList, id: \.self) { settingActionSection in
                    
                    ForEach(settingActionSection, id: \.self) { settingAction in
                        
                        if settingAction == .logout {
                            // 退出登录按钮
                            SettingLogoutCell()
                        } else {
                            
                            // 其他cell，会进一步跳转
                            NavigationLink(destination: execSettingAction(settingAction: settingAction)) {
                                SettingDefaultCell(settingAction: settingAction)
                            }
                        }
                    }
                    
                    Color(hex: 0xF1F4F7)
                        .frame(height: 16)
                        // 向上偏移1pt，为了遮盖每一组最后一个cell的底部分割线
                        .offset(y: -1)
                }
            }
        }
        .background(Color(hex: 0xF1F4F7))
        .navigationBarHidden(true)
    }
    
    private func SettingDefaultCell(settingAction: SettingAction) -> some View {
        VStack(spacing: 0) {
            HStack {
                Text(settingAction.title)
                    .foregroundColor(Color.black.opacity(0.7))
                
                Spacer()
                
                Image(systemName: "chevron.forward")
                    .foregroundColor(Color.black.opacity(0.4))
            }
            .font(.system(size: 14))
            .padding(.horizontal, 16)
            .frame(height: 45)
            
            
            Divider()
                .foregroundColor(Color.black.opacity(0.2))
                .frame(maxWidth: .infinity)
                .frame(height: 0.5, alignment: .bottom)
                .padding(.leading, 16)
                
        }
        .frame(maxWidth: .infinity)
        .background(Color.white)
    }
    
    private func SettingLogoutCell() -> some View {
        Button {
            
        } label: {
            Text("退出登录")
                .font(.system(size: 16))
                .foregroundColor(.white)
                .frame(height: 40)
                .frame(maxWidth: .infinity)
                .background(Color.red)
                .padding(.horizontal, 10)
        }
    }
}

struct SettingView_Previews: PreviewProvider {
    static var previews: some View {
        SettingView()
    }
}
